package com.beyondprototype.batch.config;

import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.beyondprototype.batch.model.CustomerMarker;
import com.beyondprototype.batch.model.PremierCustomer;

import lombok.extern.slf4j.Slf4j;

@Configuration
@EnableBatchProcessing
@Slf4j
public class BatchJobConfiguration {
	
//	@Bean
//    @Qualifier("batchJob")
//    public Job batchJob(JobBuilderFactory jobs,
//    		@Qualifier("loadGcduJsonDataStep") Step loadGcduDataStep,
//    		@Qualifier("ihubhkUpdateStep") Step ihubhkUpdateStep) {
//    	
//    	return jobs.get("batchJob").flow(loadGcduDataStep).end().build();
//    }
	
    @Bean
    @Qualifier("batchJob")
    public Job batchJob(JobBuilderFactory jobs,
    		@Qualifier(/*"loadGcduJsonDataStep"*/ "loadGcduFlat2JsonDataStep") Step loadGcduJsonDataStep,
    		@Qualifier("loadGcduFlatDataStep") Step loadGcduFlatDataStep,
    		@Qualifier("ihubhkUpdateStep") Step ihubhkUpdateStep,
    		@Qualifier("p1paCalculationStep") Step p1paCalculationStep) {
    	
    	return jobs.get("batchJob")
    			.flow(loadGcduJsonDataStep)
    			.next(loadGcduFlatDataStep)
    			.next(ihubhkUpdateStep)
    			.next(p1paCalculationStep)
    			.end()
    			.build();
    }
    
    @Bean
    @Qualifier("loadGcduFlatDataStep")
    public Step loadGcduFlatDataStep(StepBuilderFactory stepFacotry,
    		@Qualifier("gcduFlatItemReader") ItemReader<CustomerMarker> itemReader,
    		@Qualifier("gcduFlatItemWriter") ItemWriter<CustomerMarker> itemWriter,
    		@Qualifier("gcduFlatItemProcessor") ItemProcessor<CustomerMarker, CustomerMarker> itemProcessor) {
    	
    	return stepFacotry.get("loadGcduFlatDataStep").<CustomerMarker,CustomerMarker>chunk(5).reader(itemReader).processor(itemProcessor).writer(itemWriter).build();
    }
    
    @Bean
    @Qualifier("loadGcduFlat2JsonDataStep")
    public Step loadGcduFlat2JsonDataStep(StepBuilderFactory stepFacotry,
    		@Qualifier("gcduFlatPeekableItemReader") ItemReader<CustomerMarker> itemReader,
    		@Qualifier("gcduFlat2JsonItemWriter") ItemWriter<PremierCustomer> itemWriter,
    		@Qualifier("gcduFlat2JsonItemProcessor") ItemProcessor<CustomerMarker, PremierCustomer> itemProcessor) {
    	
    	return stepFacotry.get("loadGcduFlat2JsonDataStep").<CustomerMarker,PremierCustomer>chunk(5).reader(itemReader).processor(itemProcessor).writer(itemWriter).build();
    }
    
    @Bean
    @Qualifier("ihubhkUpdateStep")
    public Step ihubhkUpdateStep(StepBuilderFactory stepFacotry,
    		@Qualifier("ihubhkItemReader") ItemReader<CustomerMarker> itemReader,
    		@Qualifier("ihubhkItemWriter") ItemWriter<CustomerMarker> itemWriter, 
    		@Qualifier("ihubhkItemProcessor") ItemProcessor<CustomerMarker, CustomerMarker> itemProcessor) {
    	
    	return stepFacotry.get("ihubhkUpdateStep").<CustomerMarker,CustomerMarker>chunk(5).reader(itemReader).processor(itemProcessor).writer(itemWriter).build();
    }
    
    @Bean
    @Qualifier("loadGcduJsonDataStep")
    public Step loadGcduJsonDataStep(StepBuilderFactory stepFacotry,
    		@Qualifier("gcduJsonItemReader") ItemReader<PremierCustomer> itemReader,
    		@Qualifier("gcduJsonItemWriter") ItemWriter<PremierCustomer> itemWriter,
    		@Qualifier("gcduJsonItemProcessor") ItemProcessor<PremierCustomer, PremierCustomer> itemProcessor) {
    	
    	return stepFacotry.get("loadGcduJsonDataStep").<PremierCustomer,PremierCustomer>chunk(5).reader(itemReader).processor(itemProcessor).writer(itemWriter).build();
    }
    
    @Bean
    @Qualifier("p1paCalculationStep")
    public Step p1paCalculationStep(StepBuilderFactory stepFacotry,
    		@Qualifier("p1paItemReader") ItemReader<CustomerMarker> itemReader,
    		@Qualifier("p1paItemWriter") ItemWriter<CustomerMarker> itemWriter,
    		@Qualifier("p1paItemProcessor") ItemProcessor<CustomerMarker, CustomerMarker> itemProcessor) {
    	
    	return stepFacotry.get("p1paCalculationStep").<CustomerMarker,CustomerMarker>chunk(5).reader(itemReader).processor(itemProcessor).writer(itemWriter).build();
    }
    
}
